package com.all.play.myadapterdrag.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.all.play.myadapterdrag.R;
import com.all.play.myadapterdrag.bean.QQMessage;
import com.all.play.myadapterdrag.interfaces.ItemMoveListener;
import com.all.play.myadapterdrag.interfaces.StartDragListener;

import java.util.Collections;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/5/22.
 * Time: 2018/5/22  15:31
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements ItemMoveListener{


    private Context context;
    private List<QQMessage> list;
    private StartDragListener startDragListener;

    public MyAdapter(StartDragListener startDragListener, List<QQMessage> list) {

        this.startDragListener = startDragListener;
        this.list = list;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.listitem, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        QQMessage qqMessage = list.get(position);
        holder.iv_logo.setImageResource(qqMessage.getLogo());
        holder.tv_name.setText(qqMessage.getName());
        holder.tv_Msg.setText(qqMessage.getLastMsg());
        holder.tv_time.setText(qqMessage.getTime());

        holder.iv_logo.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //触摸时,监听event
                if (event.getAction()==MotionEvent.ACTION_DOWN){
                    //传递触摸情况给谁?--->CallBack
                    startDragListener.onStartDrag(holder);

                }

                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        //1.交换数据
        Collections.swap(list,fromPosition,toPosition);
        //2.刷新
        notifyItemMoved(fromPosition,toPosition);
        return false;//没使用
    }

    @Override
    public boolean onItemRemove(int position) {

        //1.删除数据
        //2.刷新
        //使用接口回调
        list.remove(position);
        notifyItemRemoved(position);
        return false;//没使用
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_logo;
        private TextView tv_name;
        private TextView tv_Msg;
        private TextView tv_time;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_logo = (ImageView)itemView.findViewById(R.id.iv_logo);
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_Msg = (TextView)itemView.findViewById(R.id.tv_lastMsg);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
        }
    }
}
